
package com.android.matmanual;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;



import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;


public class RTCTest extends BaseActivity 
{
	

	WakeLock mWakeLock;
	TextView mTv;
	public int mMinutes;
	public int mHour;
	public int mSeconds;
	public Handler mHandler;
	private Thread mClockThread;
	protected static final int GUINOTIFIER = 0x1234;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
     
		setContentView(R.layout.rtctest);
		setTitle(R.string.rtctest_title);
		   PowerManager pm =
		           (PowerManager) getSystemService(POWER_SERVICE); 
		    mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, 
		          "XYTEST"); 
		    mWakeLock.acquire(); 
		    mTv = (TextView)findViewById(R.id.tv);
		    mHandler = new Handler()
            {
              public void handleMessage(Message msg) 
              {
                switch (msg.what)
                { 
                  case GUINOTIFIER:
                	  handleChangeTime();
                    break;
                  
                } 
              }
            };
            mClockThread=new LooperThread();
            mClockThread.start();
	}

							
    private void handleChangeTime() {
  	  String mHourStr = "";
	  String mMinutesStr = "";
	  String mSecondsStr = "";
	  if(mHour<10){
		  mHourStr = "0" + mHour;
	  }else{
		  mHourStr = "" + mHour;
	  }
	  if(mMinutes<10){
		  mMinutesStr = "0" + mMinutes;
	  }else{
		  mMinutesStr = "" + mMinutes;
	  }
	  if(mSeconds<10){
		  mSecondsStr = "0" + mSeconds;
	  }else{
		  mSecondsStr = "" + mSeconds;
	  }
	  
	 mTv.setText(mHourStr + " : " + mMinutesStr + " : " + mSecondsStr);
	  }
    
    class LooperThread extends Thread
	  {
	    public void run() 
	    {
	      super.run();
	      try
	      {
	        do
	        {
	          
	          long time = System.currentTimeMillis();
	         
	          final Calendar mCalendar = Calendar.getInstance();
	          mCalendar.setTimeInMillis(time);
	          mHour = mCalendar.get(Calendar.HOUR_OF_DAY);
	          mMinutes = mCalendar.get(Calendar.MINUTE);
	          mSeconds = mCalendar.get(Calendar.SECOND);

	          	          
	           Message m = new Message();
	           m.what = GUINOTIFIER;
	           mHandler.sendMessage(m);
	           

	           Thread.sleep(1000); 
		           
	        }while(LooperThread.interrupted()==false);
	        
	      }
	      catch(Exception e)
	      {
	        e.printStackTrace();
	      }
	    }
	  }
    
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		int result = TestControl.GetKeyEvent(keyCode);
		if (result != TestControl.DO_NOTHING) {
			setResult(result);
			finish();
	    }
	    return true;
	}
	    @Override
	    protected void onDestroy() {
	    	// TODO Auto-generated method stub
	    	mHandler.removeMessages(GUINOTIFIER);
			if(mWakeLock!=null)
			{
				mWakeLock.release(); 
				mWakeLock = null;
			}
	    	super.onDestroy();
	    }	 
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
        finish();
		super.onStop();
	}

	@Override
	public void onAttachedToWindow() 
	{
		// TODO Auto-generated method stub
		super.onAttachedToWindow();
		this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
	}

}
		 